if(!dojo._hasResource["market.widgets.component.ContentEditor"]){
dojo._hasResource["market.widgets.component.ContentEditor"]=true;
require(["dojo/_base/declare","dijit/_Widget","dijit/form/Textarea","dojo/query","dojo/_base/array"],function(declare,_Widget,Textarea,query,array){
declare("market.widgets.component.ContentEditor",[_Widget],{
hasEdit:false,hasAttach:false,targetNode:null,value:'',editId:'',handelEvents:{},_selectedFiles:[],ignoreEdit:false,
showEditor:function(){
   if (this.ignoreEdit) 
      this._showEditorForm(this.value);
   else 
      this._showEditorWithValue(this.value);
},
setValue:function(value){
   query("pre",this.targetNode)[0].innerHTML=value;
   this.value=value;
},
getCurrentValue:function(){
   var preObject = dojo.query("pre",this.targetNode);
   if (preObject[0] != null) return preObject[0].innerHTML;
   return '';
},
_showEditorWithValue:function(oldHTMLData){
   var htmlData='<div><pre>'+oldHTMLData+'</pre></div><div class="bt_green_right">';
   if (!this.ignoreEdit)
      htmlData+='<a href="#"><img src="../images/edit.png" name="btnEdit" title="'+globalBtnEditLabel+'"/></a>';
   htmlData+='</div><div class="clear"></div>';
   dojo.html.set(this.targetNode,htmlData,{parseContent:false});
   var widget = this;
   query('img[name="btnEdit"]',this.targetNode).onclick(function(){
      widget._showEditorForm(widget.value);
      widget._handleEvent('contentEditClick');
   });
},
_showEditorForm:function(oldHTMLData){
   var htmlTextArea = this.templateHTML();
   this._innerContent(htmlTextArea);
   dijit.registry.remove(this._getEditorId());
   new dijit.form.Textarea({name:"content",value:oldHTMLData,style:"width:100%;"},this._getEditorId());
   var widget = this;
   query('img[name="btnCancel"]',this.targetNode).onclick(function(){
      widget.showEditor();
      widget._handleEvent('cancelClick');
   });
   query('img[name="btnSave"]',this.targetNode).onclick(function(){
      var contentValue = dojo.byId(widget._getEditorId()).value;
      widget.setValue(contentValue);
      widget.showEditor();
      widget._handleEvent('saveClick',contentValue);
   });
   query('img[name="btnAttach"]',this.targetNode).onclick(function(){
      widget._handleEvent('attachClick');
   });
},
_handleEvent:function(name, contentData){
   if (this.handelEvents[name]!=null)this.handelEvents[name](contentData);
},
_innerContent:function(htmlData){
   dojo.html.set(this.targetNode,htmlData,{parseContent:true});
},
_getEditorId:function(){
   if (this.editId==null||this.editId=='') this.editId='areaContent';
   return this.editId;
},
templateHTML:function(){
   var htmlTextArea = '<div style="margin:5px;"><div><pre style="display:none;height:0px">'+this.getCurrentValue()+'</pre><textarea id="'+this._getEditorId()+'"></textarea></div>';
   htmlTextArea+='<div class="bt_green_right"><a href="#"><img src="../images/undo.png" name="btnCancel" title="'+globalBtnCancelLabel+'"/></a></div>';
   if (this.hasAttach)
     htmlTextArea+='<div class="bt_green_right"><a href="#"><img src="../images/attach.png" name="btnAttach" title="'+globalBtnAttachLabel+'"/></a></div>';
   if (this.hasEdit)
     htmlTextArea+='<div class="bt_green_right"><a href="#"><img src="../images/ok.png" name="btnSave" title="'+globalBtnSaveLabel+'"/></a></div>';
   htmlTextArea+='</div><div class="clear"></div>';
   return htmlTextArea;
},
addFiles:function(){
   var _selectedFiles = SYSTEMWIDGET.showPictures().selectedFiles();
   var widget=this;
   array.forEach(_selectedFiles, function(fileGUID, index){
      var objectFile = new Object();
      objectFile.guid=fileGUID;
      objectFile.state='new';
      widget._selectedFiles.push(objectFile);
   });
},
removeFile:function(removeGUID){
   var _newSelectedFiles = new Array();
   array.forEach(this._selectedFiles, function(file, index){
      if (removeGUID==file.guid && 'new'!=file.state){
         file.state='delete';
         _newSelectedFiles.push(file);
      }else if (removeGUID!=file.guid){
         _newSelectedFiles.push(file);
      }
   });
   this._selectedFiles=_newSelectedFiles;
},
resetFiles:function(){
   var _newSelectedFiles = new Array();
   array.forEach(this._selectedFiles, function(file, index){
      var state=file.state;
      if (null==state||'delete'==state){
         file.state=null;
         _newSelectedFiles.push(file);
      }
   });
   this._selectedFiles=_newSelectedFiles;
},
clearFiles:function(){
   var _newSelectedFiles = new Array();
   array.forEach(this._selectedFiles, function(file, index){
      var state=file.state;
      if (null==state||'new'==state){
         file.state=null;
         _newSelectedFiles.push(file);
      }
   });
   this._selectedFiles=_newSelectedFiles;
}
});});}
